package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.DTO.ArticleRemarksDTO;
import com.example.demo.entity.VO.ArticleRemarksVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IArticleService;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

import com.example.demo.service.IArticleRemarksService;
import com.example.demo.entity.ArticleRemarks;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.we
 * @since 2024-06-04
 */
@Api(tags = "文章评论接口")
@RestController
@RequestMapping("/article-remarks")
public class ArticleRemarksController {

    @Resource
    private IArticleRemarksService articleRemarksService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IUserService userService;

    /**
     * 新增评论
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "新增评论")
    @PostMapping
    public Result<String> add(@RequestBody ArticleRemarksVO articleRemarksVO) {
        ArticleRemarks articleRemarks = new ArticleRemarks();
        BeanUtil.copyProperties(articleRemarksVO,articleRemarks);
        if (articleService.findArticleById(articleRemarks.getArticleId()) == null) {
            return Result.error().msg("文章不存在");
        }
        if (userService.findNicknameByUserId(articleRemarks.getUserId()) == null) {
            return Result.error().msg("用户不存在");
        }
        articleRemarksService.addRemarks(articleRemarks);
        return Result.success().msg("评论成功");
    }

    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询所有评论")
    @GetMapping
    public Result<List<ArticleRemarksDTO>> selectAllRemarks() {
        List<ArticleRemarksDTO> articleRemarksDTOS = articleRemarksService.selectAllRemarks();
        if (articleRemarksDTOS.size() == 0) {
            return Result.error().msg("没有评论");
        }
        return Result.success(articleRemarksDTOS).msg("查询成功");
    }
    //通过userId查询所有评论
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "通过userId查询所有评论")
    @GetMapping("/findAllByUserId")
    public Result<List<ArticleRemarksDTO>> findAllByUserId(int userId) {
        List<ArticleRemarksDTO> articleRemarksDTOS = articleRemarksService.findAllByUserId(userId);
        if (articleRemarksDTOS.size() == 0) {
            return Result.error().msg("该用户没有评论");
        }
        return Result.success(articleRemarksDTOS).msg("查询成功");
    }
}